//
//  HistoryLotteryViewController.m
//  DayAndDayLottery
//
//  Created by dengzhiping on 2017/6/1.
//  Copyright © 2017年 dengzhiping. All rights reserved.
//

#import "HistoryLotteryViewController.h"

@interface HistoryTableViewCell : UITableViewCell
@property (nonatomic, strong) NSArray *numberArray;

@end

@implementation HistoryTableViewCell

@end

static NSString *identifier = @"historyTableViewCellIdentifier";

@interface HistoryLotteryViewController ()
@property (weak, nonatomic) IBOutlet UITableView *historyTableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (nonatomic, strong) NSDictionary *localDataDic;
@property (nonatomic, strong) NSMutableArray *numberArray;
@property (nonatomic, strong) NSMutableArray *kjNumArray;
@property (nonatomic, strong) NSMutableArray *qiHaoArray;


@end

@implementation HistoryLotteryViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUp];
    [self initHistoryLotteryRequest];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
}

#pragma mark - Private Method
- (void)setUp{
    self.historyTableView.tableFooterView = [[UIView alloc] init];
    self.numberArray = [NSMutableArray array];
    self.kjNumArray = [NSMutableArray array];
    self.qiHaoArray = [NSMutableArray array];

    for (int i = 1; i <= 11; i++) {
        UILabel *label = [[UILabel alloc] init];
        [self.headerView addSubview:label];
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = i<10?[NSString stringWithFormat:@"0%d",i]:[NSString stringWithFormat:@"%d",i];
    }
    
    [self.headerView.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:20
                                                         fixedLineSpacing:5 fixedInteritemSpacing:5
                                                                warpCount:11
                                                               topSpacing:0 bottomSpacing:0 leadSpacing:50 tailSpacing:10];

}

- (void)initHistoryLotteryRequest{

    [self.view showLoading];
    _localDataDic = [Utils readLocalDataWithFileName:@"LotyInfo" fileType:@"json"];
    NSDictionary *kjDataDic = [_localDataDic objectForKey:@"data"];
    NSArray *kjListArray = [kjDataDic objectForKey:@"kjlist"];
    [kjListArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString  *ylz = [obj objectForKey:@"kj_ylz"];
        NSArray *numArray = [ylz componentsSeparatedByString:@","];
        [_numberArray addObject:numArray];
        
        NSString *kj_haoma = [obj objectForKey:@"kj_haoma"];
        NSArray *kjHaoMaArray = [kj_haoma componentsSeparatedByString:@","];
        [_kjNumArray addObject:kjHaoMaArray];
        
        NSString *kj_qihao = [obj objectForKey:@"kj_qihao"];
        [self.qiHaoArray addObject:kj_qihao];
        
    }];
//    [self.historyTableView reloadData];
    [self.view hideLoading];

}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 20;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _numberArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HistoryTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    NSArray *tempArray = _numberArray[indexPath.row];
    NSArray *tempkjArray = _kjNumArray[indexPath.row];
    NSMutableArray *temp1Array = [NSMutableArray arrayWithArray:tempArray];
    
    for (int j=0; j< tempkjArray.count; j++) {
        int gg = [tempkjArray[j] intValue];
        [temp1Array replaceObjectAtIndex:gg -1 withObject: tempkjArray[j]];
    }
    
    cell.numberArray = temp1Array;
    UILabel *label = [[UILabel alloc] init];
    NSString *tempStr = self.qiHaoArray[indexPath.row];
    label.text = [NSString stringWithFormat:@"%@期", [tempStr substringWithRange:NSMakeRange(tempStr.length - 2, 2)]];
    //        self.label.backgroundColor = [UIColor yellowColor];
    label.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:label];
    [ label remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(10);
        make.top.equalTo(0);
        make.width.equalTo(40);
    }];
    
    UIView *numberView = [[UIView alloc] init];
    [cell.contentView addSubview:numberView];
    [numberView remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(40);
        make.top.equalTo(0);
        make.right.equalTo(0);
    }];
    
    for (int i=0; i<cell.numberArray.count; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [numberView addSubview:label];
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = cell.numberArray[i];
        NSString *singleStr = cell.numberArray[i];
        if (singleStr.length >1) {
            label.backgroundColor = [UIColor redColor];
            label.layer.cornerRadius = 8;
            label.clipsToBounds = YES;
        }
    }
    
    [numberView.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:0
                                                    fixedLineSpacing:5 fixedInteritemSpacing:5
                                                           warpCount:11
                                                          topSpacing:0 bottomSpacing:0 leadSpacing:10 tailSpacing:10];

  
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
